package com.yumeno.epidemic.Controller;

import com.yumeno.epidemic.beans.UserInfo;
import com.yumeno.epidemic.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    public static Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(UserInfo userInfo, Model model, HttpSession session) {
        logger.debug("login()被执行了");
        UserInfo user = userService.findByAccount(userInfo.getAccount());
        if (user == null) {
            //账号不正确
            model.addAttribute("msg", "账号不正确");
            return "login";
        }
        if (user.getPassword().equals(userInfo.getPassword())) {
            //密码正确
            session.setAttribute("userLogined",user);
            return "redirect:/main.jsp";
        }
        model.addAttribute("msg", "密码错误");
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/epidemic.jsp";
    }
}
